package com.starblink.home.ui.part

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import com.starblink.android.basic.appsflyer.AppsflyerManager
import com.starblink.android.basic.data.SearchDataManager
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigation
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.common.databinding.LayoutHomeSearchBarBinding
import com.starblink.basic.route.RoutePage
import com.starblink.home.ui.NavHomeVM
import com.starblink.home.ui.adapter.HotWordFlipperAdapter
import com.starblink.rocketreserver.fragment.SearchWordVOF

/***
 * 处理頂部搜索標題
 */
class PartSearchBar(
    var vb: LayoutHomeSearchBarBinding,
    var viewModel: NavHomeVM,
) {

    fun handleVM(
        context: Context,
        owner: LifecycleOwner,
    ) {
        var flipperAdapter = HotWordFlipperAdapter(
            context,
            viewModel.searchTxtQueue.value
        )
        vb.avfView.adapter = flipperAdapter
        viewModel.searchTxtQueue.observe(owner) {
            flipperAdapter.notifyDataSetChanged()
        }

        vb.iCameraImg.trackData(
            page = SpmPageDef.Home080Page_10164,
            element = SpmElementDef.HomeNewCameraClick_33001,
            onlyHit = true
        )


        vb.iCameraImg.click {
//            context.navigation(path = RoutePage.Wishlist.WISHLIST_ENTRY)
            context.navigation(path = RoutePage.ImageSearch.PAGE_CAMERA)
            AppsflyerManager.reportCameraRecognition()
        }
        //点击前往搜索界面

        vb.searchBar.trackData(
            page = SpmPageDef.Home080Page_10164,
            element = SpmElementDef.HomeNewSearchBar_33002,
            onlyHit = true
        )

        vb.searchBar.click {
            navigationTo(url = RoutePage.Search.PAGE_SEARCH) {
                val model = vb.avfView.run {
                    var curShading: SearchWordVOF? = null
                    if (viewModel.searchTxtQueue.value?.isNotEmpty() == true) {
                        curShading = viewModel.searchTxtQueue.value!![displayedChild]
                    }
                    if (curShading == viewModel.emptySearchWord) {
                        curShading = null
                    }
                    curShading
                }
                model?.let {
                    withObject(RoutePage.Search.ARG_SHADING_WORD, it)
                }
                withObject(RoutePage.Search.ARG_HOT_WORDS, viewModel.hotWordsList)
            }
        }
        vb.accmLogOut.click { view ->
            val model = vb.avfView.run {
                var curShading: SearchWordVOF? = null
                if (viewModel.searchTxtQueue.value?.isNotEmpty() == true) {
                    curShading = viewModel.searchTxtQueue.value!![displayedChild]
                }
                if (curShading == viewModel.emptySearchWord) {
                    curShading = null
                }
                curShading
            }
            val url = model?.linkUrl
            var searchTerm = ""
            var showTerm = ""
            if (url?.isNotEmpty() == true) {
                searchTerm = url
                showTerm = url
                navigationTo(url = url)
            } else {
                model?.let {
                    showTerm = it.showWord
                    searchTerm = it.actualWord ?: ""
                }

                if (model == null || (model.actualWord.isNullOrEmpty() && model.showWord.isNullOrEmpty())) {
                    navigationTo(url = RoutePage.Search.PAGE_SEARCH)
                } else {
                    viewModel.fetchSearchWordConfig(model?.actualWord?.ifEmpty { model.showWord }
                        ?: "",
                        2) { config ->
                        navigationTo(url = RoutePage.Search.PAGE_SEARCH) {
                            model?.let {
                                extraOf(
                                    RoutePage.Search.ARG_KEY_WORD to it.actualWord?.ifEmpty { it.showWord },
                                    RoutePage.Search.GOOD_RULE_ID to config.goodsRuleId
                                )
                            }
                        }
                    }
                }
            }
            model?.let { word ->
                word.actualWord?.ifEmpty { word.showWord }?.takeIf { it.isNotEmpty() }?.run {
                    SearchDataManager.saveNormalWord(this)
                }
            }

            trackSearch(searchTerm, showTerm)
        }
    }

    private fun trackSearch(searchTerm: String, showTerm: String) {
        SpmTrackHandler.addSingleTrackData(
            SkAntEntity(
                spmCnt = GTrackerAssistUtils.fetchElementValue(
                    SpmPageDef.Home080Page_10164,
                    SpmElementDef.Element_33003
                ),
                // 给用户展现的词
                params = mapOf("searchTerm" to showTerm)
            )
        )

        SpmTrackHandler.addSingleTrackData(
            SkAntEntity(
                spmCnt = GTrackerAssistUtils.fetchElementValue(
                    SpmPageDef.Home080Page_10164,
                    SpmElementDef.SearchKeyword,
                ),
                // 实际搜索词
                params = mapOf("searchTerm" to searchTerm)
            )
        )
    }
}